import json
import traceback
from django.http import JsonResponse
from django.views.generic import View
from unit_management.models import *
from ..utils.related_util.choice_util import get_valid_unit_choices_by_dimension,get_all_valid_show_unit_choices
from utils.responseContentUtil import *


# 获取某一个量纲下的所有单位选项
class UnitOption(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = request.GET
            dimension_uuid = data.get("dimension", None)
            dimension_entity = Dimension.objects.filter(
                uuid=dimension_uuid, valid_flag=True).first()
            if not dimension_entity:
                return make_get_error_response(message="量纲不存在")
            unit_list = get_valid_unit_choices_by_dimension(dimension_entity)
            return make_get_success_response(data=unit_list)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(str(e))

# 获取某一个量纲下的所有单位选项


class QuantityDimensionUnitOption(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = request.GET
            quantity_uuid = data.get("quantity", None)
            quantity_entity = Quantity.objects.filter(
                uuid=quantity_uuid, valid_flag=True).first()
            if not quantity_entity:
                return make_get_error_response(message="量不存在")
            dimension_entity = quantity_entity.dimension
            unit_list = get_valid_unit_choices_by_dimension(dimension_entity)
            return make_get_success_response(data=unit_list)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(str(e))

class AllAvailableUnitOption(View):
    def get(self,request):
        res = {"code": 400, "message": "", "data": None}
        try:
            unit_list = get_all_valid_show_unit_choices()
            return make_get_success_response(data=unit_list)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(str(e))